package mm.util;

import java.lang.reflect.Method;
import java.util.Map;

import mm.view.User;

public class ClassicActionInvoker {

	private Method method;
	private Object controller;

	public ClassicActionInvoker(Method method, Object controller) {
		this.method = method;
		this.controller = controller;
	}
	
	public Object invoke(User user, String id, String[] sequence, Map paramMap) throws Exception{
		Class[] paramClasses = method.getParameterTypes();
		if(paramClasses==null||paramClasses.length<1){
			return method.invoke(controller, (Object[]) null);
		}
		Object[] params=new Object[paramClasses.length];
		for (int i = 0; i < params.length; i++) {
			Class paramClass = paramClasses[i];
			if (paramClass.equals(User.class)){
				params[i]=user;
			}else if (paramClass.equals(String.class)){
				params[i]=id;
			}else if (paramClass.equals(String[].class)){
				params[i]=sequence;
			}else if (paramClass.equals(Map.class)){
				params[i]=paramMap;
			}
		}
		return method.invoke(controller, params);
	}
	
	public Class getReturnType(){
		return method.getReturnType();
	}

}
